package com.cn.sku.config;

import com.cn.dataSource.Constants;
import com.cn.dataSource.DynamicDataSourceContxtHolder;
import com.sun.xml.internal.bind.v2.TODO;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.AsyncHandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Component
public class DataSourceInterceptor implements AsyncHandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //TODO
        if (Constants.DS_NAME_LIST.contains(request.getMethod())){
            DynamicDataSourceContxtHolder.setDataSourceType(request.getMethod());
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        DynamicDataSourceContxtHolder.clearDataSourceType();
    }

    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        DynamicDataSourceContxtHolder.clearDataSourceType();
    }
}
